// pages/deploy/depoly.ts
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {  
    view:0,
    snCode:"" ,
    devInfo:{},
    devDepoly:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {

  }, 
  scanSn(){
    var me = this;
    try {
      wx.scanCode({
        success (res) {
          var snCode = res.result ||"";
          me.setSnCode(snCode);
        }
      })
    } catch (error) {
      console.log(error);
    }
  },
  doOnInputSnCode(en){
    var me = this;
    try { 
      var snCode = en.detail.value||me.data.snCode;
      me.setSnCode(snCode);
    } catch (error) {
      console.log(error);
    }
  },
  setSnCode( snCode){
    var me = this;
    try { 
      me.setData({snCode:snCode},()=>{
        if( snCode==""){
          me.setData({
            view:0
          })
        }
        else{
          app.seed.callApi("depoly/getDeviceData" ,{snCode: snCode}).then(res=>{
            var view = 0;
            var devInfo = res[0] && res[0].length>0?res[0][0]:null;
            var devDepoly = res[1] && res[1].length>0?res[1][0]:null;
            if( devInfo==null){
              view = 1;
            }
            else if(devDepoly==null){
              view = 2;
            }
            else{
              view = 3;
            }
            me.setData({view ,devInfo ,devDepoly},()=>{
              console.log(me.data)
            });
          })
        }
      });
      console.log(snCode)
    } catch (error) {
      console.log(error);
    }
  } ,
  onInputSnCode( en ){
    var me = this;
    try { 
      var snCode = en.detail.value;
      me.setData({
        snCode:snCode
      })
    } catch (error) {
      console.log(error);
    }
  } , 
  acCancel() {
    this.setData({  
      view:0,
      snCode:"" ,
      devInfo:{},
      devDepoly:{}
    }); 
  },
  acDepolyDevice() {
    var me = this;
    try {  
      var data = {};
      app.seed.callApi("depoly/getDepolyDtata" ,{snCode: me.data.snCode}).then(res=>{ 
        data.customs= res[0];
        data.grains= res[1];
        data.barns= res[2];
        data.positions=[];
        data.dev= res[3][0];
        data.customInd =0;
        data.grainInd =0;
        data.barnInd =0;
        data.posInd =0;
        for( var i=0;i<20;i++){
          data.positions.push((i+1).toString()+"#");
        }
        app.globalData.depolyData = data;
        var url = "./depoly_selectCustom";
        wx.navigateTo({
          url:url
        })
      })
    } catch (error) {
      console.log(error);
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({  
      view:0,
      snCode:"" ,
      devInfo:{},
      devDepoly:{}
    }); 
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})